ListCat = 5


class MyList(object):
    """docstring for Mylist"""

    def __init__(self):
        self.item = list()
        self.cap = ListCat
        self.length = 0

    # 获取list容量
    def get_cap(self):
        return self.cap

    # 获取list长度
    def get_length(self):
        return self.length

    # 添加数据到队尾
    def push(self, data):
        self.length += 1
        self.expand_cap()
        self.item.append(data)

    # 从队尾删除数据
    def pop(self, data):
        # 判断数组内元素是否为0
        if self.length <= 0:
            print("err list is nill")
            return
        e = self.item[self.get_length() - 1]
        self.item[self.get_length() - 1] = None
        self.length -= 1

        self.reduce_cap()
        return e

    # 动态扩容方法:超过数组容量，按照当前容量的 2 倍扩容
    def expand_cap(self):
        if self.length <= self.cap:
            return
        newsize = self.get_cap() * 2
        newlist = list()
        for i in range(self.get_length() - 1):
            newlist.append(self.item[i])
        self.item = newlist
        self.cap = newsize

    # 动态缩容方法 数组元素个数为当前容量 1/4 时，缩容为当前容量的一半
    def reduce_cap(self):
        if self.length <= self.cap:
            return
        newsize = self.get_cap() / 2
        newitem = list()
        for i in range(self.get_length() - 1):
            self.item = newitem
            self.cap = newsize

    # getitem
    def getitem(self, index):
        if index < 0 or index > self.get_length() - 1:
            print("err list out of range")
            return None
        return self.item[index]

    # 这里需要重写__str__方法方便print打印
    def __str__(self):
        return "[{}]".format(self.item)